<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

require_once('lib.php');
require_once('guestbook_form.php');
defined('MOODLE_INTERNAL') || die();
require_login();
global $OUTPUT, $PAGE;

$PAGE->set_context(context_system::instance());
$PAGE->set_title("guestbook");
$PAGE->set_url('/blocks/guestbook/handler.php');
$PAGE->set_pagelayout('standard');
$PAGE->set_heading(get_string('pluginname', 'block_guestbook'));
if(isset($_GET['action'])) {
    $action = $_GET['action'];
    //в зависимости от действия выполнить
    switch ($action){
        //обработка комментария
        case "comment": 
            $form = new guestbook_form("comment.php");
            $toform = array('id' => $_GET['id'], 'action' => 'comment');
            $form->set_data($toform);
            if($form->is_cancelled()){
                $url = new moodle_url('/view.php');
                redirect($url);
            } else {
                echo $OUTPUT->header();
                $form->display();
                $face->show_message_with_comments($engine, $_GET['id']) ;
                echo $OUTPUT->footer(); 
            }
           
        break;
        //обработка редактирования
        case "edit":
            //вызов метода для получения текста редактируемой записи
            $message=$engine->get_message($_GET['tbl_name'],$_GET['id']);//print_object($message);
            //вывод формы для редактирования
            $form = new guestbook_form("edit.php") ;
            $toform = array('text' => $message->text, 'tbl_name' => $_GET['tbl_name'], 'id' => $_GET['id']);
            $form->set_data($toform);
            if($form->is_cancelled()) {
                $url = new moodle_url('/view.php');
                redirect($url);
            } else {
                echo $OUTPUT->header();
                $form->display();
                echo $OUTPUT->footer();
            }
        break;
        //обработка удаления
        case "delete":
            //вызов метода класса для удаления записи из бд
            $engine->delete($_GET['tbl_name'],$_GET['id']); 
            //перенаправления на главную страницу
            $url = new moodle_url('/blocks/guestbook/view.php');
            redirect($url);
        break;
        default: echo "Error";
    }
}


